//
//  ECMineSafeChangeNameVc.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/15.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#define Textfield_height        960*0.1406         //封装输入框的高度

#import "ECMineSafeChangeNameVc.h"
#import "ECUITextField_Account.h"

@interface ECMineSafeChangeNameVc (){
    
    ECUITextField_Account *uitf_name;
    ECUButton *loginBtn;
}

@end

@implementation ECMineSafeChangeNameVc

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户名";
    
    uitf_name = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(60), kProportion_Width(60), kScreenWidth - kProportion_Width(MarginLeft_Textfield*2) , kProportion_Height(Textfield_height)) placeholder:@"用户名" leftImage:[UIImage imageNamed:@"registered_userName"]];
    [self.view addSubview:uitf_name];
    uitf_name.textField.text = self.nickName;
    [uitf_name.textField becomeFirstResponder];
    
    // login btn
    loginBtn = [ECUButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorNormal"] forState:UIControlStateNormal];
    loginBtn.frame = CGRectMake(kProportion_Width(60), CGRectGetMaxY(uitf_name.frame) + kProportion_Height(MarginHeight_textfield), kScreenWidth - kProportion_Width(60*2),(kScreenWidth - kProportion_Width(60*2))* 0.1406);
    [loginBtn addTarget:self action:@selector(action_Login) forControlEvents:UIControlEventTouchUpInside];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorSelected"] forState:UIControlStateHighlighted];
    [loginBtn setTitle:@"确定" forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:ceil(20)];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [self.view addSubview:loginBtn];
    
    UILabel *tipLab = [[UILabel alloc]init];
    [self.view addSubview:tipLab];
    tipLab.text = @"可直接用于登录";
    tipLab.textColor = [UIColor colorWithHexString:@"#484848"];
    tipLab.textAlignment = NSTextAlignmentCenter;
    
    WeakSelf(weakSelf);
    [self.view updateConstraintsIfNeeded];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(kProportion_Height(60));
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.height.equalTo(@15);
        make.width.equalTo(@150);
    }];
    
}

-(void)action_Login{
    [self getDataWithURL];
}

/*  请求数据  */
-(void)getDataWithURL{
    DLog(@"%@",[[UserInfoManager shareUser] token]);
    [HWHttpTool requestWithMethod:@"POST"
                              url:URL_POST_UpdateNickname
                           params:@{@"nickname":uitf_name.textField.text
                                    }
                          headers:@{@"X-AUTH-TOKEN":[[UserInfoManager shareUser] token]}
                          success:^(id json) {
                              DLog(@"%@",json);
                              [MBProgressHUD showTipMessageInWindow:@"用户名修改成功"];
                              [[UserInfoManager shareUser]setNickName:uitf_name.textField.text];//存储
                              [self.navigationController popViewControllerAnimated:YES];
                          }
                          failure:^(NSError *error) {
                              NSString *str = [ECData_Json requestErrorTipWith:error];
                              DLog(@"%@",str);
                              if (!([str isEqualToString:@"101"] || [str isEqualToString:@"401"])) {
                                  [MBProgressHUD showTipMessageInWindow:str];
                              }
                          }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
